import java.util.ArrayList;
import java.util.Scanner;
public class Studentitem62 {
    public static void startStudentSystem() {
        ArrayList<Studentitem> students = new ArrayList<>();
     loop:while (true) {
        System.out.println("-------------欢迎来到学生管理系统---------------");
        System.out.println("1:添加学生");
        System.out.println("2:删除学生");
        System.out.println("3:修改学生");
        System.out.println("4:查询学生");
        System.out.println("5:退出");
        System.out.println("请输入你的选择: ");
        Scanner sc = new Scanner(System.in);
        String s = sc.next();
            switch (s) {
                case "1"-> addStudent(students);
                case "2"-> deleteStudent(students);
                case "3"-> repairStudent(students);
                case "4"-> checkStudent(students);
                case "5"-> {System.out.println("退出");
                    break loop;}
                default -> System.out.println("没有这个选项");
            }
        }
    }
    public static void addStudent(ArrayList<Studentitem> students) {
        System.out.println("添加学生");
        Studentitem s = new Studentitem();
        Scanner sc = new Scanner(System.in);
        while(true) {
            System.out.println("请输入学生id");
            String id = sc.next();
            boolean flag = flag(students, id);
            if (flag) {
                System.out.println("id已经存在,请重新录入");
            }else {
                s.setId(id);
                break;
            }
        }
        System.out.println("请输入学生的姓名");
        String name = sc.next();
        s.setName(name);
        System.out.println("请输入学生年龄");
        int age = sc.nextInt();
        s.setAge(age);
        System.out.println("请输入学生地址");
        String address = sc.next();
        s.setAddress(address);
        students.add(s);
    }
    public static void deleteStudent(ArrayList<Studentitem> students) {
        System.out.println("删除学生");
        Scanner sc =new Scanner(System.in);
        String id = sc.next();
        System.out.println("请输入要删除的id");
        int a = getIndex(students, id);
        if(a>=0){
            students.remove(a);
            System.out.println("id为"+id+"的学生删除成功");
        }else{
            System.out.println("id为"+id+"的学生不存在");
        }
    }
    public static void repairStudent(ArrayList<Studentitem> students){
        System.out.println("修改学生");
        Scanner sc =new Scanner(System.in);
        String id = sc.next();
        System.out.println("请输入要修改学生的id");
        int a = getIndex(students, id);
        if(a<0){
            System.out.println("您输入的学生id不存在,请重新输入");
            return;
        }
        else{
            Studentitem s = students.get(a);
            System.out.println("请输入修改的学生姓名");
            String newName = sc.next();
            s.setName(newName);
            System.out.println("请输入修改的学生年龄");
            int newAge = sc.nextInt();
            s.setAge(newAge);
            System.out.println("请输入要修改的学生地址");
            String newAddress = sc.next();
            s.setAddress(newAddress);
            System.out.println("您已修改完成");
        }
    }
    public static void checkStudent(ArrayList<Studentitem> students) {
        System.out.println("查询学生");
        if(students.size()==0){
            System.out.println("当前无学生信息,请添加后查询");
            return;
        }
        System.out.println("id\t姓名\t年龄\t家庭住址");
        for(int i=0; i<students.size(); i++){
            Studentitem stu = students.get(i);
            System.out.println(stu.getId()+"\t"+stu.getName()+"\t"+stu.getAge()+"\t"+stu.getAddress());
        }
    }
    public static boolean flag(ArrayList<Studentitem> students,String id){
        for(int i=0; i<students.size(); i++){
            Studentitem stu1 = students.get(i);
            String uid = stu1.getId();
            if(id.equals(uid)){
                return true;
            }
        }
        return false;
    }
    public static int getIndex(ArrayList<Studentitem> students,String id){
        for(int i=0; i<students.size(); i++){
            Studentitem stu1 = students.get(i);
            String uid = stu1.getId();
            if(id.equals(uid)){
                return i;
            }
        }
        return -1;
    }
}